home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Utilities / Post / Source / Post.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-03  |  13.4 KB  |  531 lines

  1. #include "PostPre.h"
  2. #include "Global.h"
  3. #include "Global.c"
  4.  
  5. #include <dos/dostags.h>
  6. #include <proto/utility.h>
  7.  
  8. #define    TICKS_TO_DELAY    5
  9. #define    SCROLL_INC        15
  10.  
  11. void    __regargs __chkabort(void);
  12. void    __regargs __chkabort(void) { return; }
  13.  
  14. int    main (int argc, char **argv)
  15. {
  16.     ULONG        rec_sig;
  17.     int        i, idcmpupdate_ticks = 0;
  18.     int        (*func)(void);
  19.     struct    Message *msg;
  20.     struct    MenuItem *tempitem;
  21.     UWORD        code;
  22.     char        subtaskname[30];
  23.  
  24.     strcpy(pubscreenname, STD_PUBSCREEN);
  25.  
  26.     Options            = DefaultOptions;
  27.     IffOptions        = DefaultOptions;
  28.     PrintOptions    = DefaultOptions;
  29.     NewOptions        = DefaultOptions;
  30.  
  31.     init_list(&DefaultOptions.startuplist);
  32.     init_list(&Options.startuplist);
  33.     init_list(&IffOptions.startuplist);
  34.     init_list(&PrintOptions.startuplist);
  35.     init_list(&NewOptions.startuplist);
  36.     LoadPostOptions("ENV:POST", &Options);
  37.     LoadPostOptions("ENV:POST", &NewOptions);
  38.     LoadPostOptions("ENV:POST.iff", &IffOptions);
  39.     LoadPostOptions("ENV:POST.print", &PrintOptions);
  40.  
  41.     strcpy(pubscreenname, Options.Screen.pubscrname);
  42.     if(open_libs() == FALSE)
  43.     {
  44.         close_libs();
  45.         return(20);
  46.     }
  47.  
  48.     Forbid();
  49.     do
  50.     {
  51.         post_count++;
  52.         sprintf(subtaskname, "post%d.render", post_count);
  53.     } while (FindTask(subtaskname));
  54.     Permit();
  55.  
  56.     if(parse(argc, NULL))
  57.     {
  58.         close_libs();
  59.         return(20);
  60.     }
  61.  
  62.     RexxStuff=InitARexx(AREXXPORTNAME, AREXXEXTENSION);
  63.  
  64.     if(argwindow)
  65.     {
  66.         filerequester = (struct FileRequester *) AllocAslRequestTags(ASL_FileRequest,
  67.                         ASLFR_SleepWindow,    TRUE,
  68.                         ASLFR_PrivateIDCMP,    TRUE,
  69.                         ASLFR_DoPatterns,        TRUE,
  70.                         ASLFR_InitialPattern,(ULONG) "(#?.ps|#?.eps|#?.epsf)",
  71.                         ASLFR_InitialDrawer,    (ULONG) Options.lastdir,
  72.                         ASLFR_InitialFile,     (ULONG) FilePart(Options.lastfile),
  73.                         ASLFR_InitialHeight, 300,
  74.                         TAG_DONE);
  75.         filereq = filerequester;
  76.         if (filerequester == NULL)
  77.         {
  78.             okmsg("can't get file requester");
  79.             errorende();
  80.             return(20);
  81.         }
  82.         fontrequester = (struct FileRequester *) AllocAslRequestTags(ASL_FileRequest,
  83.                         ASLFR_SleepWindow,    TRUE,
  84.                         ASLFR_PrivateIDCMP,    TRUE,
  85.                         ASLFR_DoPatterns,        TRUE,
  86.                         TAG_DONE);
  87.         if (fontrequester == NULL)
  88.         {
  89.             okmsg("can't get file requester");
  90.             errorende();
  91.             return(20);
  92.         }
  93.     }
  94.     if((mainport = CreatePort(NULL, 0)) == NULL)
  95.     {
  96.         okmsg("can't create messageport!");
  97.         errorende();
  98.         return(20);
  99.     }
  100.  
  101.     menumsg.ExecMessage.mn_ReplyPort = mainport;
  102.     menumsg.ExecMessage.mn_Node.ln_Type = NT_MESSAGE;
  103.     menumsg.ExecMessage.mn_Length = sizeof(struct PSmessage);
  104.     rendertask = CreateNewProcTags(NP_Entry, (ULONG) SubTask,
  105.                     NP_Name, (ULONG) subtaskname, NP_StackSize, 4096,
  106.                     NP_Priority, 0, NP_Input, Input(), NP_Output, Output(),
  107.                     NP_Error, Output(), NP_CloseInput, FALSE, NP_CloseOutput,
  108.                     FALSE, NP_CloseError, FALSE, TAG_DONE);
  109.     if(rendertask)
  110.     {
  111.         Wait(1L << create_signal);
  112.         if (renderport == NULL)
  113.         {
  114.             okmsg("subtask can't create messageport!");
  115.             errorende();
  116.             return(20);
  117.         }
  118.         if(argwindow) SetTaskPri((struct Task *) rendertask, 0);
  119.         else SetTaskPri((struct Task *) rendertask, -1);
  120.     }
  121.     else
  122.     {
  123.         okmsg("can't create subtask");
  124.         errorende();
  125.         return(20);
  126.     }
  127.  
  128. /* Open the screen and load the color map  */
  129.  
  130.     if(Options.Screen.custscreen && argwindow) openscreen();
  131.     else if (argwindow)
  132.     {
  133.         if(openpubscreen(&pubscrcontext, pubscreenname) == FALSE)
  134.         {
  135.             errorende();
  136.             exit(20);
  137.         }
  138.         if(pubscrcontext.pens_ok == FALSE) Options.PostOpts.depth = 1;
  139.         ScreenToFront(pubscrcontext.pubscreen);
  140.     }
  141.  
  142.     colormap.Count = 1 << Options.PostOpts.depth;
  143.     if(Options.PostOpts.depth == 1) colormap.ColorTable = (APTR) bcolors;
  144.     else colormap.ColorTable = (APTR) ccolors;
  145.  
  146.     if(argwindow)
  147.     {
  148.         sprintf(windowtitle, "Post.%d (Waiting, Page --- of ---, no file loaded)", post_count);
  149.         openwindow();
  150.     }
  151.  
  152.     if(argfile)
  153.     {
  154.         if(number_of_pages)
  155.         {
  156.             if (Options.PostOpts.from < 1) Options.PostOpts.from = 1;
  157.             if (Options.PostOpts.to > number_of_pages) Options.PostOpts.to = number_of_pages;
  158.             if (Options.PostOpts.from > Options.PostOpts.to) Options.PostOpts.from = Options.PostOpts.to;
  159.             page_number = Options.PostOpts.from;
  160.         }
  161.         else Options.PostOpts.from = 1;
  162.         if ((argprint || argiff) && Options.PostOpts.bandrendering)
  163.         {
  164.             if(Options.PostOpts.bandsize < Options.PostOpts.height)
  165.             {
  166.                 if(number_of_pages == 0) bandrendering = FALSE;
  167.                 else bandrendering = TRUE;
  168.             }
  169.         }
  170.     }
  171.     sendmenu(PSACTCREATE, 0, 0, 0, NULL, NULL);
  172.     Wait(1L << create_signal);
  173.     if(createerror)
  174.     {
  175.         okmsg("subtask can't create activation!");
  176.         errorende();
  177.         return(20);
  178.     }
  179.     runstartupfiles();
  180.     if(argfile)
  181.     {
  182.         sendmenu(PSACTFILE, number_of_pages, Options.PostOpts.from, Options.PostOpts.to, Options.lastfile, Options.lastdir);
  183.     }
  184.  
  185. /* Execute menu commands */
  186.  
  187.     if (argwindow)
  188.     {
  189.         for (;;)
  190.         {
  191.             signals = ARexxSignal(RexxStuff);
  192.             if(OutputWnd) signals |= 1L << OutputWnd->UserPort->mp_SigBit;
  193.             signals |= 1L << mainport->mp_SigBit;
  194.             rec_sig = Wait(signals);
  195.             if(rec_sig == 1L << mainport->mp_SigBit)
  196.             {
  197.                 while(msg = GetMsg(mainport))
  198.                 {
  199. /* Check if action == PSACTAREXX and reply the ARexxMsg. So Arexx scripts    *
  200.  * can be synchronised with the interpreter.                                            */
  201.                     if(((struct PSmessage *) msg)->action == PSACTAREXX)
  202.                     {
  203.                         ReplyARexxMsg(RexxStuff,((struct PSmessage *) msg)->rmsg,0,retcode ? 5 : 0);
  204.                     }
  205.                     FreeMem(msg, sizeof(struct PSmessage));
  206.                 }
  207.             }
  208.             while(rmsg = GetARexxMsg(RexxStuff))
  209.             {
  210.                 ProcessARexx();
  211.             }
  212.             if(OutputWnd)
  213.             {
  214.                 while ((intmsg = (struct IntuiMessage *) GetMsg(OutputWnd->UserPort)))
  215.                 {
  216.                     switch (((struct IntuiMessage *) intmsg)->Class)
  217.                     {
  218.                     case IDCMP_MOUSEMOVE:
  219.                         idcmpupdate_ticks = 0;
  220.                         get_scrollers();
  221.                         do_blit();
  222.                         break;
  223.  
  224.                     case IDCMP_GADGETDOWN:
  225.                         idcmpupdate_ticks = 0;
  226.                         do_gadgets(((struct Gadget *) intmsg->IAddress)->GadgetID, 1);
  227.                         break;
  228.  
  229.                     case IDCMP_IDCMPUPDATE:
  230.                         idcmpupdate_ticks++;
  231.                         if(idcmpupdate_ticks < TICKS_TO_DELAY) break;
  232.                         do_gadgets(GetTagData(GA_ID, 0, (struct TagItem *) intmsg->IAddress), SCROLL_INC);
  233.                         break;
  234.  
  235.                     case IDCMP_MENUVERIFY:
  236.                         setmenu();
  237.                         break;
  238.  
  239.                     case IDCMP_SIZEVERIFY:
  240.                         blit_to_window = FALSE;
  241.                         break;
  242.  
  243.                     case IDCMP_CLOSEWINDOW:
  244.                         Delay(10);
  245.                         if(!interactive && !startup)
  246.                         {
  247.                             SavePostOptions("ENV:Post", &Options);
  248.                             errorende();
  249.                             return(0);
  250.                         }
  251.                         break;
  252.  
  253.                     case IDCMP_REFRESHWINDOW:
  254.                         BeginRefresh(OutputWnd);
  255.                         do_blit();
  256.                         EndRefresh(OutputWnd, TRUE);
  257.                         break;
  258.  
  259.                     case IDCMP_ACTIVEWINDOW:
  260.                         SetTaskPri((struct Task *) rendertask, 0);
  261.                         break;
  262.  
  263.                     case IDCMP_INACTIVEWINDOW:
  264.                         SetTaskPri((struct Task *) rendertask, -1);
  265.                         break;
  266.  
  267.                     case IDCMP_NEWSIZE:
  268.                         winxsize = OutputWnd->Width - OutputWnd->BorderLeft - OutputWnd->BorderRight;
  269.                         winysize = OutputWnd->Height - OutputWnd->BorderTop-OutputWnd->BorderBottom;
  270.                         set_scrollers();
  271.                         do_blit();
  272.                         break;
  273.  
  274.                     case IDCMP_MENUPICK:
  275.                         code = intmsg->Code;
  276.                         if (code != MENUNULL)
  277.                         {
  278.                             tempitem = ItemAddress(menu0, code);
  279.                             func = (void *) (GTMENUITEM_USERDATA( tempitem ));
  280.                             i = func();
  281.                         }
  282.                         break;
  283.  
  284.                     case IDCMP_CHANGEWINDOW:
  285.                         if(OutputWnd->Flags & WFLG_ZOOMED)
  286.                         {
  287.                             Options.OutputWnd.WndZoomLeft        = OutputWnd->LeftEdge;
  288.                             Options.OutputWnd.WndZoomTop        = OutputWnd->TopEdge;
  289.                             Options.OutputWnd.WndZoomWidth    = winxsize + OutputWnd->BorderLeft + OutputWnd->BorderRight;
  290.                             Options.OutputWnd.WndZoomHeight    = winysize + OutputWnd->BorderTop + OutputWnd->BorderBottom;
  291.                         }
  292.                         else
  293.                         {
  294.                             Options.OutputWnd.WndLeft            = OutputWnd->LeftEdge;
  295.                             Options.OutputWnd.WndTop            = OutputWnd->TopEdge;
  296.                             Options.OutputWnd.WndWidth            = winxsize;
  297.                             Options.OutputWnd.WndHeight        = winysize;
  298.                         }
  299.                         break;
  300.  
  301.                     case IDCMP_RAWKEY:
  302.                         switch(intmsg->Code & 0x7f)
  303.                         {
  304.                             case    CURSORUP:
  305.                                 if ((intmsg->Qualifier & IEQUALIFIER_LSHIFT) ||
  306.                                     (intmsg->Qualifier & IEQUALIFIER_RSHIFT))
  307.                                 do_gadgets(GAD_UP, (9*winysize)/10);
  308.                                 else if (intmsg->Qualifier & IEQUALIFIER_CONTROL)
  309.                                     do_gadgets(GAD_UP, parm.page.ysize - winysize);
  310.                                 else    do_gadgets(GAD_UP, 1);
  311.                                 break;
  312.  
  313.                             case    CURSORDOWN:
  314.                                 if ((intmsg->Qualifier & IEQUALIFIER_LSHIFT) ||
  315.                                     (intmsg->Qualifier & IEQUALIFIER_RSHIFT))
  316.                                     do_gadgets(GAD_DOWN, (9*winysize)/10);
  317.                                 else if (intmsg->Qualifier & IEQUALIFIER_CONTROL)
  318.                                     do_gadgets(GAD_DOWN, parm.page.ysize - winysize);
  319.                                 else    do_gadgets(GAD_DOWN, 1);
  320.                                 break;
  321.  
  322.                             case    CURSORLEFT:
  323.                                 if ((intmsg->Qualifier & IEQUALIFIER_LSHIFT) ||
  324.                                     (intmsg->Qualifier & IEQUALIFIER_RSHIFT))
  325.                                     do_gadgets(GAD_LEFT, (9*winxsize)/10);
  326.                                 else if (intmsg->Qualifier & IEQUALIFIER_CONTROL)
  327.                                     do_gadgets(GAD_LEFT, parm.page.xsize - winxsize);
  328.                                 else    do_gadgets(GAD_LEFT, 1);
  329.                                 break;
  330.  
  331.                             case    CURSORRIGHT:
  332.                                 if ((intmsg->Qualifier & IEQUALIFIER_LSHIFT) ||
  333.                                     (intmsg->Qualifier & IEQUALIFIER_RSHIFT))
  334.                                     do_gadgets(GAD_RIGHT, (9*winxsize)/10);
  335.                                 else if (intmsg->Qualifier & IEQUALIFIER_CONTROL)
  336.                                     do_gadgets(GAD_RIGHT, parm.page.xsize - winxsize);
  337.                                 else    do_gadgets(GAD_RIGHT, 1);
  338.                                 break;
  339.  
  340.                             default:
  341.                                 break;
  342.                         }
  343.                         break;
  344.  
  345.                     case IDCMP_VANILLAKEY:
  346.                         switch(intmsg->Code)
  347.                         {
  348.                             case    ' ':
  349.                                 if(winypos < parm.page.ysize - winysize)
  350.                                     do_gadgets(GAD_DOWN, (9*winysize)/10);
  351.                                 else
  352.                                 {
  353.                                     if(nextpage() == 0) do_gadgets(GAD_UP, parm.page.ysize - winysize);
  354.                                 }
  355.                                 break;
  356.  
  357.                             case    13:
  358.                             case    '+':
  359.                                 i = !nextpage();
  360.                                 if((intmsg->Qualifier & IEQUALIFIER_NUMERICPAD) && i)
  361.                                     do_gadgets(GAD_UP, parm.page.ysize - winysize);
  362.                                 break;
  363.  
  364.                             case    8:
  365.                             case    '-':
  366.                                 i = !previouspage();
  367.                                 if((intmsg->Qualifier & IEQUALIFIER_NUMERICPAD) && i)
  368.                                     do_gadgets(GAD_DOWN, parm.page.ysize - winysize);
  369.                                 break;
  370.  
  371.                             default:
  372.                                 break;
  373.                         }
  374.                         break;
  375.  
  376.                         default:
  377.                         break;
  378.                     }
  379.                     if(intmsg) ReplyMsg((struct Message *) intmsg);
  380.                 }
  381.             }
  382.         }
  383.     }
  384.     sendmenu(PSACTWAIT, 0, 0, 0, NULL, NULL);
  385.     Wait(1L << create_signal);
  386.     errorende();
  387.     return(0);
  388. }
  389.  
  390. BPTR    oldwblock = NULL;
  391.  
  392. BOOL open_libs(void)
  393. {
  394.     BPTR    lock = NULL;
  395.  
  396.     GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 39);
  397.     if(GfxBase) os_3 = TRUE;
  398.     else GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 36);
  399.     IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 36);
  400.     AslBase = OpenLibrary("asl.library", 36);
  401.     GadToolsBase = OpenLibrary("gadtools.library", 36);
  402.     UtilityBase = OpenLibrary("utility.library", 36);
  403.     DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 36);
  404.     if (GfxBase == NULL || IntuitionBase == NULL || AslBase == NULL
  405.         || GadToolsBase == NULL || UtilityBase == NULL || DOSBase == NULL)
  406.     {
  407.         okmsg("Can't open libraries! You need OS 2.0!!!");
  408.         return(FALSE);
  409.     }
  410.     PSbase = OpenLibrary("post.library", 17L);
  411.     if (PSbase == NULL)
  412.     {
  413.         okmsg("Can't open post.library >= V17!!!");
  414.         return(FALSE);
  415.     }
  416.     maintask = FindTask(NULL);
  417.     create_signal = AllocSignal(-1);
  418.     if(create_signal == -1)
  419.     {
  420.         okmsg("can't get create_signal!");
  421.         return(FALSE);
  422.     }
  423.     GetCurrentDirName(startdir, 256);
  424.     if (_WBenchMsg)
  425.     {
  426.         if (lock = Lock(startdir, ACCESS_READ))
  427.         {
  428.             oldwblock = CurrentDir(lock);
  429.         }
  430.         else
  431.         {
  432.             okmsg("can't get lock!");
  433.             return(FALSE);
  434.         }
  435.     }
  436.     insertbreakmain();
  437.     SetExcept(~0, SIGBREAKF_CTRL_C);
  438.     return(TRUE);
  439. }
  440.  
  441. void close_libs(void)
  442. {
  443.     BPTR    lock;
  444.  
  445.     SetExcept(0, SIGBREAKF_CTRL_C);
  446.     deletebreakmain();
  447.     if (!_WBenchMsg) changedir(startdir);
  448.     else
  449.     {
  450.         lock = CurrentDir(oldwblock);
  451.         if (lock) UnLock(lock);
  452.     }
  453.     if (PSbase)  CloseLibrary((struct Library *) PSbase);
  454.     if (GadToolsBase) CloseLibrary((struct Library *) GadToolsBase);
  455.     if (AslBase) CloseLibrary((struct Library *) AslBase);
  456.     if (GfxBase) CloseLibrary((struct Library *) GfxBase);
  457.     if (DOSBase) CloseLibrary((struct Library *) DOSBase);
  458.     if (IntuitionBase) CloseLibrary((struct Library *) IntuitionBase);
  459.     if (UtilityBase) CloseLibrary(UtilityBase);
  460.     if (create_signal != -1) FreeSignal(create_signal);
  461. }
  462.  
  463. void errorende(void)
  464. {
  465.     struct Message *msg;
  466.  
  467.     if(rendertask)
  468.     {
  469.         if(renderport)
  470.         {
  471.             saveterminate();
  472.             SetTaskPri((struct Task *) rendertask, 10);
  473.             sendmenu(PSACTCLOSE, 0, 0, 0, NULL, NULL);
  474.             Wait(1L << create_signal);
  475.         }
  476.     }
  477.     while(msg = GetMsg(mainport))
  478.     {
  479.         if(((struct PSmessage *) msg)->action == PSACTAREXX)
  480.         {
  481.             ReplyARexxMsg(RexxStuff,((struct PSmessage *) msg)->rmsg,0,retcode);
  482.         }
  483. /*        sprintf(buffer, "action = %d", ((struct PSmessage *)msg)->action);
  484.         okmsg(buffer);*/
  485.         FreeMem(msg, sizeof(struct PSmessage));
  486.     }
  487.     if(rmsg) ReplyARexxMsg(RexxStuff, rmsg, 0, 0);
  488.     while(rmsg = GetARexxMsg(RexxStuff)) ReplyARexxMsg(RexxStuff, rmsg, 0, 0); 
  489.     FreeARexx(RexxStuff);
  490.     if(pubscrcontext.ps_lock)
  491.     {
  492.         UnlockPubScreen(NULL, pubscrcontext.pubscreen);
  493.         pubscrcontext.ps_lock = FALSE;
  494.     }
  495.     closewindow();
  496.     if (custscreen)
  497.     {
  498.         while(!CloseScreen(custscreen)) okmsg("you have to close the visitor windows!");
  499.         custscreen = NULL;
  500.     }
  501.     OpenWorkBench();
  502.     delete_tempfiles();
  503.     if(filerequester) FreeAslRequest(filerequester);
  504.     if(fontrequester) FreeAslRequest(fontrequester);
  505.  
  506.     if (mainport)
  507.     {
  508.         DeletePort(mainport);
  509.         mainport = NULL;
  510.     }
  511.     close_libs();
  512. }
  513.  
  514. void    saveterminate(void)
  515. {
  516.  
  517.     if(running)
  518.     {
  519.         if(number_of_pages == 0 || paused == 0) PSsignalint(arec, 2);
  520.         if(paused)
  521.         {
  522.             Signal((struct Task *) rendertask, 1L << pause_signal);
  523.             if (number_of_pages == 0) sendmenu(PSACTPAUSE, 0, 0, 0, NULL, NULL);
  524.         }
  525.         number_of_pages = 0;
  526.         while (running) Delay(10);
  527.     }
  528. }
  529.  
  530. /* End of file "post.c" */
  531.